/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include "cc2420.h"


/* ------------------------------------------------------------------------------------------------
 *                                    Common Radio Defines
 * ------------------------------------------------------------------------------------------------
 */
#if 0
#define MAC_RF_CHANNEL_DEFAULT               11
#define MAC_RF_TX_POWER_DEFAULT              0x7F
#define MAC_RF_TX_POWER_MAX_MINUS_DBM        25

#define MAC_RF_RECEIVER_SENSITIVITY_DBM      -95 /* dBm */
#define MAC_RF_RECEIVER_SATURATION_DBM       10  /* dBm */

#define MAC_RF_RSSI_OFFSET                   HAL_MAC_RSSI_OFFSET

#define MAC_RF_RX_TX_PROP_DELAY_MIN_USEC     3.076  /* usec */
#define MAC_RF_RX_TX_PROP_DELAY_MAX_USEC     3.284  /* usec */

#endif
#define HAL_TIMER_TICKS_PER_USEC                  	HAL_CPU_CLOCK_MHZ
/* Microseconds in one symbol */
#define MAC_SPEC_USECS_PER_SYMBOL           		16
/* The number of symbols forming a basic CSMA-CA time period */
#define MAC_A_UNIT_BACKOFF_PERIOD       			20
/* Microseconds in one backoff period */
#define MAC_SPEC_USECS_PER_BACKOFF          		(MAC_SPEC_USECS_PER_SYMBOL * MAC_A_UNIT_BACKOFF_PERIOD)


/* ------------------------------------------------------------------------------------------------
 *                                    Common Radio Macros
 * ------------------------------------------------------------------------------------------------
 */
//#define RF_INIT()        						cc2420_init()
#define RF_RESET()								cc2420_reset()

#define RF_ON()                     			CC2420_VREG_ACTIVE()
#define RF_OFF()                    			CC2420_VREG_INACTIVE()
#define RF_OSC_ON()                       		cc2420_Oscillator_TurnOn()
#define RF_OSC_OFF()                      		cc2420_command(CC2420_SXOSCOFF)

#define RF_RX_OVERFLOWED()            			CC2420_RX_IS_OVERFLOW()
#define RF_CCA_CHECK()							CC2420_CCA_CHECK()
#define RF_CHANNEL_CLEAR()						CC2420_CCA_IS_ACTIVE()
#define RF_TX_UNDERFLOWED()						CC2420_TX_IS_UNDERFLOW()
#define RF_TX_IS_ACTIVE()                      	(cc2420_status() & CC2420_MS_TX_ACTIVE)

#define RF_WAIT_RSSI_VALID()					CC2420_WAIT_RSSI_VALID()
#define RF_WAIT_TX_END()						CC2420_WAIT_TX_END()
#define RF_SET_RX_THRESHOLD(x)                 	cc2420_change_reg(CC2420_REG_IOCFG0,0,CC2420_IOC0_FIFOP_THR_MASK,(x)-1)
#define RF_ENABLE_FIFOP_INT()     				cc2420_enable_fifop()
#define RF_DISABLE_FIFOP_INT()    				cc2420_disable_fifop()
#define RF_RSSI_VALUE()							((int8)(cc2420_read_register(CC2420_REG_RSSI_CCA) & CC2420_RSSI_VAL_MASK))
#define RF_TX_ACK()                            	cc2420_command(CC2420_SACK)
#define RF_TX_ACK_PEND()                       	cc2420_command(CC2420_SACKPEND)
#define RF_RX_ON()                         		cc2420_command(CC2420_SRXON)
#define RF_TX_ON()								cc2420_command(CC2420_STXON)
#define RF_TX_CCA_ON()							cc2420_command(CC2420_STXONCCA)

#define RF_RXTX_OFF()                      		cc2420_command(CC2420_SRFOFF)
#define RF_FLUSH_TX_FIFO()                     	cc2420_flush_Txfifo()
#define RF_FLUSH_RX_FIFO()                     	cc2420_flush_Rxfifo()


#define RF_READ_RX_FIFO(pData,len)             	cc2420_read_fifo(pData, (uint8)(len))
#define RF_WRITE_TX_FIFO(pData,len)            	cc2420_write_fifo(pData, (uint8)(len))

#define RF_SET_PAN_COORDINATOR(b)              	cc2420_change_reg(CC2420_REG_MDMCTRL0,CC2420_MC0_PAN_COORDINATOR_SHIFT,CC2420_MC0_PAN_COORDINATOR_MASK,b)
#define RF_SET_CHANNEL(x)                      	cc2420_write_register(CC2420_REG_FSCTRL, (uint16)(CC2420_FSCTRL_BASE_VALUE + (CC2420_FSCTRL_FREQ_2405MHZ + 5 * ((x) - 11))))
#define RF_SET_TX_POWER(x)                     	cc2420_write_register(CC2420_REG_TXCTRL, (uint16)(CC2420_TXCTRL_BASE_VALUE | (x)))
#define RF_GET_TX_POWER()						((int8)(cc2420_read_register(CC2420_REG_TXCTRL) & CC2420_TXCTRL_TXPOWER_MASK))

#define RF_SET_PAN_ID(p)                       	cc2420_write_ram(CC2420_RAM_PANID,2,p)
#define RF_GET_PAN_ID(p)						cc2420_read_ram(CC2420_RAM_PANID,2,p);
#define RF_SET_SHORT_ADDR(p)                   	cc2420_write_ram(CC2420_RAM_SHORTADR,2,p)
#define RF_GET_SHORT_ADDR(p)					cc2420_read_ram(CC2420_RAM_SHORTADR,2,p);
#define RF_SET_IEEE_ADDR(p)                    	cc2420_write_ram(CC2420_RAM_IEEEADR,8,p)
#define RF_GET_IEEE_ADDR(p)						cc2420_read_ram(CC2420_RAM_IEEEADR,8,p);

#define RF_RANDOM_BYTE()                       	cc2420_random_byte()

#define RF_TIMER_TICKS_PER_USEC()              	HAL_TIMER_TICKS_PER_USEC /* may be fractional */
#define RF_TIMER_TICKS_PER_BACKOFF()           	((uint16) (HAL_TIMER_TICKS_PER_USEC * MAC_SPEC_USECS_PER_BACKOFF + 0.5))
#define RF_TIMER_TICKS_PER_SYMBOL()            	((uint16) (HAL_TIMER_TICKS_PER_USEC * MAC_SPEC_USECS_PER_SYMBOL + 0.5))

#define RF_ENABLE_ADR_FILTER()        			cc2420_change_reg(CC2420_REG_MDMCTRL0,11,CC2420_MC0_ADR_DECODE_MASK,1)
#define RF_DISABLE_ADR_FILTER()       			cc2420_change_reg(CC2420_REG_MDMCTRL0,11,CC2420_MC0_ADR_DECODE_MASK,0)

#define RF_ENABLE_AUTOACK()						cc2420_change_reg(CC2420_REG_MDMCTRL0,4,CC2420_MC0_AUTOACK_MASK,1)
#define RF_DISABLE_AUTOACK()					cc2420_change_reg(CC2420_REG_MDMCTRL0,4,CC2420_MC0_AUTOACK_MASK,0)

#define RF_SET_BCN_ACCEPT_ALL()					cc2420_change_reg(CC2420_REG_IOCFG0,11,CC2420_IOC0_BCN_ACCEPT,1)
#define RF_CLR_BCN_ACCEPT_ALL()					cc2420_change_reg(CC2420_REG_IOCFG0,11,CC2420_IOC0_BCN_ACCEPT,0)
